<?php
/**
 * NetBiz Framework
 * Copyright (C) 20009 - 2010 NetBiz
 * 
 * All rights reserved.
 */

if (FRM_Config::get('SESSION','HANDLER_TYPE') === "DATABASE") {
	require_once(APPLICATION_DIR."/model/Sessions.php");
}

class FRM_Session
{
	static protected $_initialized = false;
	static protected $_regenerate = false;

	static public function initialize()
	{
		if (!self::$_initialized)
		{
			if (FRM_Config::get('SESSION','HANDLER_TYPE') === "DATABASE")
			{
				session_set_save_handler(
										array('FRM_Session', 'open'),
										array('FRM_Session', 'close'),
										array('FRM_Session', 'read'),
										array('FRM_Session', 'write'),
										array('FRM_Session', 'destroy'),
										array('FRM_Session', 'gc'));
			}

			session_name(FRM_Config::get('APPLICATION','NAME'));
			session_start();
			self::$_initialized = true;

			if (self::$_regenerate)
			{
				session_regenerate_id();
				self::$_regenerate = FALSE;
			}
		}
	}

	function __construct()
	{
	}

	static public function open($path, $id)
	{
		//return true;
	}

	static public function close()
	{
		//return true;
	}

	static public function read($id)
	{
		$session = new DataObjects_Sessions();
		$session->id = $id;
		$session->selectAdd();
		$session->selectAdd("data");

		$return = '';
		if ($session->find(TRUE))
		{
			$return = $session->data;
			$session->free();
		}

		return $return;
	}

	static public function write($id, $data)
	{
		$access = time();
	
		$session = new DataObjects_Sessions();
		$id = $session->escape($id);
		$access = $session->escape($access);
		$data = $session->escape($data);
	
		$res = $session->query(
			"REPLACE
				INTO    ".$session->tableName()."
				VALUES  ('$id', '$access', '$data')"
			);
	
		return ($res instanceof DB_Error) ? false : true;
	}

	static public function destroy($id = null)
	{
		if (!is_null($id))
		{
			// Handler call, not user call
			$session = new DataObjects_Sessions();
			$session->id = $id;
			return $session->delete();
		}
		else
		{
			self::remove();
			session_destroy();
			self::$_regenerate = TRUE;
			self::$_initialized = FALSE;
		}
	}
	
	static public function gc($maxlifetime)
	{
		$session = new DataObjects_Sessions();

		$old = time() - $maxlifetime;
		$old = $session->escape($old);
	
		$session->whereAdd("access < '$old'");
		return $session->delete(true);
	}
	
	static public function reset($varname)
	{
		if (!self::$_initialized)
		{
			self::initialize();
		}
		unset($_SESSION[$varname]);
	}

	static public function set($varname, $value = '')
	{
		if (!self::$_initialized)
		{
			self::initialize();
		}
		$_SESSION[$varname] = $value;
	}

	static public function get($varname)
	{
		if (!self::$_initialized)
		{
			self::initialize();
		}
		return isset($_SESSION[$varname]) ? $_SESSION[$varname] : '';
	}
	
	static public function exist($varname)
	{
		if (!self::$_initialized)
		{
			self::initialize();
		}
		return isset($_SESSION[$varname]);
	}

	static public function remove()
	{
		if (!self::$_initialized)
		{
			self::initialize();
		}

		foreach($_SESSION as $key => $v)
		{
			unset($_SESSION[$key]);
		}
	}

	static public function regenerateId()
	{
		if (!self::$_initialized)
		{
			self::initialize();
		}
		session_regenerate_id();
	}
}